from django.shortcuts import render, get_object_or_404
from django.contrib import auth
# Create your views here.
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse, HttpResponseRedirect
from django.views.decorators.csrf import csrf_exempt

from sign.models import Event, Guest
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger


def index(requests):
    return render(requests, "index.html")


def login_action(requests):
    if requests.method == 'POST':
        username = requests.POST.get('username', '')
        password = requests.POST.get('password', '')
        user = auth.authenticate(username=username, password=password)
        if user is not None:
            auth.login(requests, user)
            requests.session['user'] = username
            respons = HttpResponseRedirect('/event_manage/')
            return respons
        else:
            return render(requests, 'index.html', {'error': 'username or password error!'})


@login_required
def event_manage(requests):
    # username = requests.COOKIES.get('use', '') #读取浏览器cookie
    event_list = Event.objects.all()
    username = requests.session.get('user', '')
    return render(requests, "event_Mange.html", {"user": username, "events": event_list})


@login_required
def search_name(requests):
    username = requests.session.get('user', '')
    search_name1 = requests.GET.get('name', "")
    event_list = Event.objects.filter(name__contains=search_name1)
    return render(requests, "event_Mange.html", {"user": username, "events": event_list})


@login_required
def guest_search(requests):
    username = requests.session.get('user', '')
    guest_search = requests.GET.get('realname', "")
    print(guest_search)
    guest_list = Guest.objects.filter(realname__contains=guest_search)
    return render(requests, "guest_Manage.html", {"user": username, "guests": guest_list})


@login_required
def guest_manage(requests):
    username = requests.session.get('user', '')
    guest_list = Guest.objects.all()
    paginator = Paginator(guest_list, 2)
    page = requests.GET.get('page')
    try:
        contacts = paginator.page(page)
    except PageNotAnInteger:
        contacts = paginator.page(1)
    except EmptyPage:
        contacts = paginator.page(paginator.num_pages)
    return render(requests, "guest_Manage.html", {"user": username, "guests": contacts})


@login_required
def sign_index(requests, eid):
    event = get_object_or_404(Event, id=eid)
    return render(requests, 'sign_index.html', {'event': event})


# 签到动作
@login_required
@csrf_exempt
def sign_index_action(requests, eid):
    event = get_object_or_404(Event, id=eid)
    phone = requests.POST.get('phone', '')
    print(phone)
    result = Guest.objects.filter(phone=phone)
    if not result:
        return render(requests, 'sign_index.html', {'event': event, 'hint': 'phone error.'})

    result = Guest.objects.filter(phone=phone, event_id=eid)
    if not result:
        return render(requests, 'sign_index.html', {'event': event, 'hint': 'event id or phone error.'})

    result = Guest.objects.get(phone=phone, event_id=eid)
    if result.sign:
        return render(requests, 'sign_index.html', {'event': event, 'hint': 'user has sign in.'})
    else:
        Guest.objects.filter(phone=phone, event_id=eid).update(sign='1')
        return render(requests, 'sign_index.html', {'event': event, 'hint': "sign in success", 'guest': result})


@login_required
def logout(requests):
    auth.logout(requests)
    response = HttpResponseRedirect('/index/')
    return response
